package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.store.client.PackageManageClient;
import com.woniuxy.store.client.param.AddFunctionParam;
import com.woniuxy.store.client.param.AddPackFunParam;
import com.woniuxy.store.client.param.AddPackageParam;
import com.woniuxy.store.infra.model.Package;
import com.woniuxy.store.infra.model.PackageFunction;
import com.woniuxy.store.service.FunctionService;
import com.woniuxy.store.service.PackageFunctionService;
import com.woniuxy.store.service.PackageService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.Result;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping(("/package"))
public class PackageManageClientImpl implements PackageManageClient {

    @Resource
    private PackageService packageService;

    @Resource
    private FunctionService functionService;

    @Resource
    private PackageFunctionService packageFunctionService;

    @Override
    public Result addPackage(@Valid AddPackageParam param) {
        Package bean = BeanUtil.toBean(param, Package.class);
        packageService.addPackage(bean);
        return Result.success();
    }

    @Override
    public Result addFunction(@Valid AddFunctionParam param) {
        functionService.addFunction(param.getFunctionName());
        return Result.success();
    }

    @Override
    public Result addPackFun(@Valid AddPackFunParam param) {
        PackageFunction packageFunction = BeanUtil.toBean(param, PackageFunction.class);
        packageFunctionService.addPackFun(packageFunction);
        return Result.success();
    }
}
